package com.jiyun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.jiyun.model.Course;
import com.jiyun.model.Quiz;
import com.jiyun.model.Train;
import com.jiyun.model.User;
import com.jiyun.service.CourseService;
import com.jiyun.service.QuizService;
import com.jiyun.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("index")
public class IndexController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TrainService trainService;

    @Autowired
    private QuizService quizService;

    @RequestMapping("count")
    public List<Long> count(@RequestParam String token) {
        Object o = redisTemplate.opsForValue().get(token);

        String s = JSONObject.toJSONString(o);

        String o1 = JSONObject.parse(s).toString();
        User user = JSONObject.parseObject(o1,User.class);

        return courseService.showCount(user.getId());
    }

    @RequestMapping("courseList")
    public List<Course> courseList(){
        return courseService.courseList();
    }


    @RequestMapping("dian")
    public void dian(@RequestParam Integer id){
        Course course = courseService.getById(id);
        course.setGood(course.getGood()+1);
        courseService.updateById(course);
        redisTemplate.delete(id+"");
        redisTemplate.boundValueOps(id+"").set(JSONObject.toJSONString(course));
    }


//    统计课程信息
    @RequestMapping("courseNum")
    public List<Course>courseNum(){
        return courseService.courseNum();
    }

//    课程点赞
    @RequestMapping("dianGood")
    public void dianGood(@RequestParam Integer id){
        Object o = redisTemplate.boundValueOps(id + "").get();
        if (o==null){
            Course course = courseService.getById(id);
            course.setGood(course.getGood()+1);
            courseService.updateById(course);
            redisTemplate.boundValueOps(id+"").set(JSONObject.toJSONString(course));
        }else{
            redisTemplate.delete(id+"");
            Course course = courseService.getById(id);
            course.setGood(course.getGood()-1);
            courseService.updateById(course);
        }

    }

//    统计最新培训信息
    @RequestMapping("trainCourse")
    public List<Train>trainCourse(){
        return trainService.trainCourse();
    }

//   统计最新测验信息
    @RequestMapping("quizList")
    public List<Quiz>quizList(){
        return quizService.quizList();
    }




}
